const { err, toast, get } = require("../../utils/wxapi");
const api = require("../../api/mop");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navNumber: 0,
    nav: [
      { title: "未审核", isEntry: 0 },
      { title: "已通过", isEntry: 1 },
      { title: "未通过", isEntry: 2 },
    ],
    page: 1,
    list: [],
    total: 0,
    mobile: "",
    role: get("user").role,
    uId: get("user").userId,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getlist();
  },
  async scres() {
    try {
      this.setData({ page: 1 });
      const { page, nav, navNumber, mobile, list } = this.data;
      const userId = wx.getStorageSync("userId");
      const data = JSON.stringify({
        mobile,
        userId,
        isEntry: nav[navNumber].isEntry,
      });
      const res = await api.entrylist({ page, data });
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        const total = res.pageTotal;
        const updatelist = page > 1 ? [...list, ...newlist] : newlist;
        console.log(updatelist);
        this.setData({ list: updatelist, total });
      }
    } catch (error) {
      err(error);
    }
  },
  nav_click(e) {
    const { i } = e.currentTarget.dataset;
    this.setData({ navNumber: i, page: 1 });
    this.getlist();
  },
  async getlist() {
    try {
      const { page, nav, navNumber, list, uId, role } = this.data;
      let data = {
        isEntry: nav[navNumber].isEntry,
      };
      if (role == 3) {
        data = {
          ...data,
          uId,
        };
      }
      const datas = JSON.stringify(data);
      const res = await api.entrylist({ page, data: datas });
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        const total = res.pageTotal;
        const updatelist = page > 1 ? [...list, ...newlist] : newlist;
        console.log(updatelist);
        this.setData({ list: updatelist, total });
      }
    } catch (error) {
      err(error);
    }
  },
  qx(e) {
    const { id, isentry } = e.currentTarget.dataset;
    wx.showModal({
      title: "提示",
      content: "确定取消该申请吗？",
      complete: (res) => {
        if (res.confirm) {
          const data = JSON.stringify({ id, isEntry: isentry });
          api
            .Pentry(data)
            .then((res) => {
              console.log(res);
              if (res.respCode === 200) {
                toast("已将该申请取消");
                this.setData({ page: 1 });
                this.getlist();
              } else {
                toast(res.msg);
              }
            })
            .catch((error) => {
              err(error);
            });
        }
      },
    });
  },
  tg(e) {
    const { id, isentry } = e.currentTarget.dataset;
    wx.showModal({
      title: "提示",
      content: "确定通过该申请吗？",
      complete: (res) => {
        if (res.confirm) {
          const data = JSON.stringify({ id, isEntry: isentry });
          api
            .Pentry(data)
            .then((res) => {
              console.log(res);
              if (res.respCode === 200) {
                toast("已将该申请通过");
                this.setData({ page: 1 });
                this.getlist();
              } else {
                toast(res.msg);
              }
            })
            .catch((error) => {
              err(error);
            });
        }
      },
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { page, total, list } = this.data;
    if (list.length < total) {
      this.setData({ page: page + 1 });
      this.getlist();
    } else {
      toast("暂无更多内容");
    }
  },
});
